package com.xjh.t.impl;

import com.xjh.t.domain.Role;
import com.xjh.t.mapper.RoleMapper;
import com.xjh.t.service.IRoleService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.io.Serializable;
import java.util.List;

/**
 * <p>
 *  服务实现类
 * </p>
 *
 * @author jobob
 * @since 2020-02-28
 */
@Service
@Transactional
public class RoleServiceImpl extends ServiceImpl<RoleMapper, Role> implements IRoleService {

    @Override
    public boolean removeById(Serializable id) {

        this.getBaseMapper().deleteRolePermissionByRid(id);
        this.getBaseMapper().deleteRoleUserByRid(id);

        return super.removeById(id);
    }

    @Override
    public List<Integer> queryRolePermissionIdsByRid(Integer roleId) {
        return this.getBaseMapper().queryRolePermissionIdsByRid(roleId);
    }

    @Override
    public void saveRolePermission(Integer rid, Integer[] ids) {

        RoleMapper roleMapper = this.getBaseMapper();
        roleMapper.deleteRolePermissionByRid(rid);
        if(ids != null && ids.length > 0) {
            for (Integer pid : ids) {
                roleMapper.saveRolePermission(rid, pid);
            }
        }
    }

    /**
     * 查询当前用户拥有的角色ID集合
     */
    @Override
    public List<Integer> queryUserRoleIdsByUid(Integer id) {
        return this.getBaseMapper().queryUserRoleIdsByUid(id);
    }

    @Override
    public void deleteRoleUserByUid(Serializable id) {
        this.getBaseMapper().deleteRoleUserByUid(id);
    }

    @Override
    public void insertUserRole(Integer uid, Integer rid) {
        this.getBaseMapper().insertUserRole(uid,rid);
    }
}
